<?php

namespace App\Http\Controllers\Admin;

use App\Http\Query\CategoryQuery;
use app\Model\Base;
use app\Model\OrderModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Log;
use function MongoDB\BSON\toJSON;
use SplFileObject;

class JapanInvoiceController extends Controller

{

    function invoice_list(Request $request)
    {
        $getParam = $request->all();

        $map = [];
        if (!empty($getParam['corp_id'])) {
            $map['uo.corp_id'] = $getParam['corp_id'];
        }
//        $db = DB::table('user_order as uo')
//            ->select(DB::raw('uod.company_name,uo.actual_money,smm.storehouse_id,smm.storehouse_name,uod.deliver_time,uo.name,uod.company_name,uod.courier_number,cm.category_name,gm.goods_name,gm.goods_id,gmm.color,gmm.size,uo.add,uo.order_id,uod.goods_num'))
//            ->leftJoin('user_order_detailed as uod', 'uo.order_id', '=', 'uod.order_id')
//            ->leftJoin('goods_master as gm', 'uod.goods_id', '=', 'gm.goods_id')
//            ->leftJoin('goods_model as gmm', 'gm.goods_id', '=', 'gmm.goods_id')
//            ->leftJoin('supplier_master as sm', 'sm.supplier_id', '=', 'gm.supplier_id')
//            ->leftJoin('category_master as cm', 'gm.category_id', '=', 'cm.category_id')
//            ->leftJoin('storehouse_detailed as sd', 'sd.goods_id', '=', 'gm.goods_id')
//            ->leftJoin('storehouse_master as smm', 'smm.storehouse_id', '=', 'sd.storehouse_id')
//            ->where($map);
        $db = DB::table('user_order as uo')->where($map)
            ->select(DB::raw('uo.*,sum(uod.goods_num) as total_num,uod.courier_number'))
            ->leftJoin('user_order_detailed as uod', 'uo.order_id', '=', 'uod.order_id')
        ->groupBy(['order_id']);

        if (!empty($getParam['star_date']) && !empty($getParam['end_date'])) {
            $list = $db->whereBetween('uod.deliver_time', [$getParam['star_date'], $getParam['end_date']])->get();
        } else {
            $list = $db->get();
        }
//        dump($getParam);
//        dump($list);
        $mo = new \App\Model\Base();
        return view('warehouse.invoice', array(
            'corpList' => $mo::companys(),
            'param' => $getParam,
            'list' => $list,
        )); // 返回视图

    }
}
